




#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

#include <assert.h>
#include <pthread.h>


#include "counter.h"
#include "ldp.h"



int fd_ldp = -1;

extern int LdpInit(void)
{
	int err = -1;
	fd_ldp = open("/dev/ldp",O_RDWR);
	if(-1 == fd_ldp)
	{
		printf("[ldp.c] do not open /dev/ldp \n");
		return -1;
	}
	else
	{
		if(-1 == LdpEnableSet(CHANNEL1, DISABLE))		return -1;
		if(-1 == LdpEnableSet(CHANNEL2, DISABLE))		return -1;
		if(-1 == LdpLedSet(ENABLE))		return -1;
		return 0;
	}
}

extern int LdpLedSet(int arg)
{
	int err = -1;
	if(arg == ENABLE)
	{
		err = ioctl(fd_ldp, IOCTL_LED_WRITE, 0);
		if(err == -1)
		{
			if(PRINT_OPEN_LDP) printf("[LDP.c] can not write led to 1 \n");
			return -1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		err = ioctl(fd_ldp, IOCTL_LED_WRITE, 1);
		if(err == -1)
		{
			if(PRINT_OPEN_LDP) printf("[LDP.c] can not write led to 0 \n");
			return -1;
		}
		else
		{
			return 0;
		}
	}
	
}
// set channel enable
extern int LdpEnableSet(unsigned int channel, int arg)
{
	int err;
	if(channel == CHANNEL1)
	{
		err = ioctl(fd_ldp, IOCTL_LDP_ENABLE_WRITE_1, arg);
		if(err == -1)
		{
			if(PRINT_OPEN_LDP) printf("[LDP.c] can not write ldp_enable_write_1 %d \n",arg);
			return -1;
		}
		else
		{
			return 0;
		}
	}
	else if(channel ==CHANNEL2)
	{
		err = ioctl(fd_ldp, IOCTL_LDP_ENABLE_WRITE_2, arg);
		if(err == -1)
		{
			if(PRINT_OPEN_LDP) printf("[LDP.c] can not write ldp_enable_write_2 %d \n",arg);
			return -1;
		}
		else
		{
			return 0;
		}
		
	}
}

extern int LdpLMFaultRead(unsigned int channel)
{
	unsigned long result;
	if(channel == CHANNEL1)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_LM_FAULT_READ_1,NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}
	else if(channel == CHANNEL2)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_LM_FAULT_READ_2,NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}
}

extern int LdpInterLockRead(unsigned int channel)
{
	unsigned long result;
	if(channel == CHANNEL1)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_INTERLOCK_READ_1,NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}
	else if(channel == CHANNEL2)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_INTERLOCK_READ_2,NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}	
}


extern int LdpTempFaultRead(unsigned int channel)
{
	unsigned long result;
	if(channel == CHANNEL1)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_TEMP_FAULT_READ_1, NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}
	else if(channel == CHANNEL2)
	{
		result = ioctl(fd_ldp, IOCTL_LDP_TEMP_FAULT_READ_2, NULL);
		if(result == 0)
			return 0;
		else
			return 1;
	}	
}







